package com.aijk.commons.lock.perf;


import com.aijk.commons.lock.Lock;
import com.aijk.commons.lock.RedisServer;
import com.aijk.commons.lock.impl.RedisLock;
import com.aijk.perf.Perf;
import redis.clients.jedis.JedisCluster;

import java.util.concurrent.TimeUnit;

public class IDGeneatorPerf {

    private static final String LOCK_KEY = "lock.lock";
    private static final long LOCK_EXPIRE = 5 * 1000;

    public static void main(String[] args) throws Exception {


        Perf perf = new Perf() {

            @Override
            public TaskInThread buildTaskInThread() {
                return new TaskInThread() {
                    JedisCluster cluster = RedisServer.getConfigCluster();
                    Lock lock = new RedisLock(cluster, LOCK_KEY, LOCK_EXPIRE);

                    @Override
                    public void initTask() throws Exception {

                    }

                    @Override
                    public void doTask() throws Exception {
                        if (lock.tryLock(3, TimeUnit.SECONDS)) {
                            try {

                            } finally {
                                lock.unlock();
                            }
                        } else {
                            throw new RuntimeException("获取锁失败");
                        }

                    }
                };
            }

        };
        perf.loopCount = 100000;
        perf.threadCount = 4;
        perf.logInterval = 1000;
        perf.run();
        perf.close();

    }
}
